// gfx_skitchin.cpp

#include "gfx_skitchin.h"

SRGB ColorRGB( unsigned int r, unsigned int g, unsigned int b )
{
     SRGB convrgb = { r, g, b };
     
     return convrgb;
}

SRect RectXYWH( float x, float y, unsigned int w, unsigned int h )
{
     SRect convrect = { x, y, w, h };
     
     return convrect;     
}


CAbstractDrawingInterface::CAbstractDrawingInterface( )
{
	bDoubleBuffer = true;
	iFPSCounter = 0;
	iFPS2Display = 0;
	iFPSDelayTime = 0;
	iFPSLimit = 60;

	tFPSDelayTimer.start();
	tFPSTimer.start();
}

void CAbstractDrawingInterface::CalcFPS( )
{
	if( tFPSTimer.getSecElapsed( ) >= 1 )
	{
		iFPS2Display = iFPSCounter;
		iFPSCounter = 0;
		tFPSTimer.restart( );
	}
	iFPSCounter++;
}

void CAbstractDrawingInterface::ThrottleFPS( )
{
	iFPSDelayTime = ( 1000 / iFPSLimit ) - tFPSDelayTimer.getMSElapsed( );

	if( iFPSDelayTime > 0 )
	{
		tFPSDelayTimer.waitMS( iFPSDelayTime );
	}

	tFPSDelayTimer.restart( );
}

bool CAbstractDrawable::IsOverlapping( CAbstractDrawable* obj )
{
	float left1, right1, top1, bottom1;
	float left2, right2, top2, bottom2;

	left1 = this->GetX();
	left2 = obj->GetX( );
	right1 = this->GetX() + this->GetW();
	right2 = obj->GetX() + obj->GetW();
	top1 = this->GetY();
	top2 = obj->GetY();
	bottom1 = this->GetY() + this->GetH();
	bottom2 = obj->GetY() + obj->GetH();

	if (bottom1 < top2) return false;
	if (top1 > bottom2) return false;

	if (right1 < left2) return false;
	if (left1 > right2) return false;

	return true;
};

int CAbstractDrawable::SnapBounds( CAbstractDrawable* obj )
{
	unsigned int snapflag = 0;
	float left1, right1, top1, bottom1;
	float left2, right2, top2, bottom2;

	left1 = this->GetX();
	left2 = obj->GetX();
	right1 = this->GetX() + this->GetW();
	right2 = obj->GetX() + obj->GetW();
	top1 = this->GetY();
	top2 = obj->GetY();
	bottom1 = this->GetY() + this->GetH();
	bottom2 = obj->GetY() + obj->GetH();

	if ( top1 < top2 )
	{
		this->SetY( top2 );
		snapflag |= (1 << 1); //Top
	}

	if ( bottom1 > bottom2 )
	{
		this->SetY( bottom2 - this->GetH() );
		snapflag |= (1 << 2); //Bottom
	}

	if ( right1 > right2 )
	{
		this->SetX( right2 - this->GetW() );
		snapflag |= (1 << 3); //Right
	}

	if ( left1 < left2 )
	{
		this->SetX( left2 );
		snapflag |= (1 << 4); //Left
	}

	return snapflag;
};
